package com.server.dashboard;

import com.dao.chart.ChartsConfigMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.model.bo.Screen;
import com.model.po.ChartsConfigToDash;
import com.model.pojo.RepCode;
import com.model.pojo.RepEntity;
import com.model.vo.configVo.*;
import com.server.chart.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;

/*
看板中调用生成图表
 */
@Service
public class DashboardsToChartsUtilService {
    @Autowired
    ChartsConfigMapper chartsConfigMapper;
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    ShowHistogramService showHistogramService;
    @Autowired
    ShowIndividualService showIndividualService;
    @Autowired
    ShowLineService showLineService;
    @Autowired
    ShowPieService showPieService;
    @Autowired
    ShowPopulationService showPopulationService;
    @Autowired
    ShowScatterService showScatterService;

    private int chartId;

    public RepEntity getChartsInDash(ChartsToDashInfo chartsToDashInfo, String token) throws SQLException {
        chartId = chartsToDashInfo.getDashCreateId();
        System.out.println("看板生成图表");
        //拿到图表配置
        ChartsConfigToDash chartsConfigToDash = chartsConfigMapper.getChartConfigToDash(chartId);
        if (chartsConfigToDash == null || "".equals(chartsConfigToDash)){
            return new RepEntity(RepCode.Null);
        }
        String chartType = chartsConfigToDash.getChartType();
        String fetchConfig = chartsConfigToDash.getFetchConfig();
        if (fetchConfig == null || "".equals(fetchConfig)){
            return new RepEntity(RepCode.Null);
        }
        //判断图表类型
        List<Screen> filters = chartsToDashInfo.getFilters();
        if ("Pie".equals(chartType)){
            return getPie(fetchConfig, token, filters);

        }else if ("Histogram".equals(chartType)){
            return getHistogram(fetchConfig, token, filters);

        }else if ("Line".equals(chartType)){
            return getLine(fetchConfig, token, filters);

        }else if ("population".equals(chartType)){
            return getPopulation(fetchConfig, token, filters);

        }else if ("individual".equals(chartType)){
            return getIndividual(fetchConfig, token, filters);

        }else if("scatter".equals(chartType)){
            return getScatter(fetchConfig, token, filters);
        }
        return new RepEntity(RepCode.success);
    }

    public RepEntity getHistogram(String fetchConfig, String token, List<Screen> filters){
        HistogramConfigInfo histogramConfigInfo = new HistogramConfigInfo();
        try {
            histogramConfigInfo = objectMapper.readValue(fetchConfig,HistogramConfigInfo.class);
            histogramConfigInfo.setFilters(filters);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return showHistogramService.showHistogram(histogramConfigInfo, token, chartId);
    }

    public RepEntity getScatter(String fetchConfig, String token, List<Screen> filters){
            ScatterConfigInfo scatterConfigInfo = new ScatterConfigInfo();
            try {
                System.out.println(fetchConfig);
                scatterConfigInfo = objectMapper.readValue(fetchConfig,ScatterConfigInfo.class);
                scatterConfigInfo.setFilters(filters);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return showScatterService.showScatter(scatterConfigInfo, token, chartId);
    }

    public RepEntity getLine(String fetchConfig, String token, List<Screen> filters){
        LineConfigInfo lineConfigInfo = new LineConfigInfo();
        try {
            System.out.println(fetchConfig);
            lineConfigInfo = objectMapper.readValue(fetchConfig,LineConfigInfo.class);
            lineConfigInfo.setFilters(filters);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return showLineService.showLine(lineConfigInfo, token, chartId);
    }

    public RepEntity getPopulation(String fetchConfig, String token, List<Screen> filters) throws SQLException {
        PopulationInfo populationInfo = new PopulationInfo();
        try {
            System.out.println(fetchConfig);
            populationInfo = objectMapper.readValue(fetchConfig,PopulationInfo.class);
            populationInfo.setFilters(filters);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return showPopulationService.showPopulation(populationInfo, token, chartId);
    }

    public RepEntity getIndividual(String fetchConfig, String token, List<Screen> filters) throws SQLException {
        IndividualConfigInfo individualConfigInfo = new IndividualConfigInfo();
        try {
            System.out.println(fetchConfig);
            individualConfigInfo = objectMapper.readValue(fetchConfig,IndividualConfigInfo.class);
            individualConfigInfo.setFilters(filters);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return showIndividualService.showIndividual(individualConfigInfo, token, chartId);
    }

    public RepEntity getPie(String fetchConfig, String token, List<Screen> filters){
        PieConfigInfo pieConfigInfo = new PieConfigInfo();
        try {
            System.out.println(fetchConfig);
            pieConfigInfo = objectMapper.readValue(fetchConfig,PieConfigInfo.class);
            pieConfigInfo.setFilters(filters);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return showPieService.showPie(pieConfigInfo, token, chartId);
    }

}
